export const baseUrl =  'https://www.atlansic.com' // 'http://127.0.0.1:5000'

const util = {
  login() {
    return new Promise((resolve, reject) => {
      uni.login({
        provider: 'weixin',
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },
  getUserInfo() {
    return new Promise((resolve, reject) => {
      uni.getUserInfo({
        provider: 'weixin',
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },
  request(option) {
    const token = uni.getStorageSync('token')
    let url = option.url
    if (!/^http/.test(url)) url = baseUrl + url
    return new Promise((resolve, reject) => {
      uni.request({
        header: { Authorization: `Bearer ${token}` },
        url,
        data: option.data,
        method: option.type || 'GET',
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },
  getStorage(key) {
    return new Promise((resolve, reject) => {
      uni.getStorage({
        key,
        success(res) {
          resolve(res.data)
        },
        fail(err) {
          reject(err)
        }
      })
    }).catch(err => console.log(err))
  },
  setStorage(key, data) {
    return new Promise((resolve, reject) => {
      uni.setStorage({
        key,
        data,
        success(res) {
          resolve(res.data)
        },
        fail(err) {
          reject(err)
        }
      })
    }).catch(err => console.log(err))
  },
  req: {
    type: {
      GET: 'GET',
      POST: 'POST',
      PUT: 'PUT',
      DELETE: 'DELETE'
    }
  }
}


export default util
